"use restrict";
const Promise = require('bluebird');
const request = require("superagent");
const charset = require('superagent-charset');
// const suretry = require('superagent-retry');
charset(request);
// suretry(request);

module.exports = {
    get: (u, p, d) => {
        return new Promise((resolve, reject) => {
            request.get(u)
                .set(p['header'] || {})
                .query(d || p['query'] || {})
                .charset(p['charset'] || "utf8")
                .retry(p['retry'] || 5)
                .end((error, result) => {
                    error ? reject(error) : resolve(result);
                });
        });
    },
    post: (u, p, d) => {
        return new Promise((resolve, reject) => {
            request.post(u)
                .set(p['header'] || {})
                .query(d || p['query'] || {})
                .charset(p['charset'] || "utf8")
                .retry(p['retry'] || 5)
                .end((error, result) => {
                    error ? reject(error) : resolve(result);
                });
        });
    },
    mpost: (u,p,d) =>{
        return new Promise((resolve, reject) => {
            request.post(u)
                .set(p['header'] || {})
                .field(d)
                .charset(p['charset'] || "utf8")
                .retry(p['retry'] || 5)
                .end((error, result) => {
                    error ? reject(error) : resolve(result);
                });
        });
    }
}